/**
 * 
 */
package com.antilia.infomaresme.pages.register;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.util.collections.MiniMap;
import org.apache.wicket.util.template.PackageTextTemplate;
import org.apache.wicket.util.template.TextTemplate;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.StringValidator;

import com.antilia.infomaresme.EnumDropDownChoice;
import com.antilia.infomaresme.data.dao.interfaces.IUsersDao;
import com.antilia.infomaresme.data.dataobjects.Language;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.data.dataobjects.UserRole;
import com.antilia.infomaresme.form.ShinyForm;
import com.antilia.infomaresme.mail.MailUtil;
import com.google.inject.Inject;

/**
 * @author Ernesto
 *
 */
public abstract class RegisterPanel extends Panel {

	private static final long serialVersionUID = 1L;

	
	private String password1;
	
	private String password2;
	
	@Inject
	private IUsersDao usersDao;
	
	private FeedbackPanel feedback;
	
	private User user;
	
	TextField<String> userId;
	
	/**
	 * @param id
	 */
	public RegisterPanel(String id) {
		super(id);		
		user = new User();
		user.setUserRole(UserRole.PLAIN);
		this.setOutputMarkupId(true);
		Form<User> form = new ShinyForm<User>("form", new CompoundPropertyModel<User>(user));		
		add(form);
		
		TextField<String> name = createTextField("name", true);
		name.setLabel(new ResourceModel("register.name"));
		form.add(name);
		
		userId = createTextField("userId", true);
		userId.setLabel(new ResourceModel("register.userId"));
		form.add(userId);
		
		EnumDropDownChoice<Language> language = new EnumDropDownChoice<Language>("language", Language.class);
		language.setRequired(true);
		language.setLabel(new ResourceModel("register.language"));
		form.add(language);
		
		TextField<String> lastName1 = createTextField("lastName1", true);
		lastName1.setLabel(new ResourceModel("register.lastName1"));
		form.add(lastName1);
		
		TextField<String> lastName2 = createTextField("lastName2", false);
		lastName2.setLabel(new ResourceModel("register.lastName2"));
		form.add(lastName2);
		
		TextField<String> email = createTextField("email", true);
		email.add(EmailAddressValidator.getInstance());
		email.setLabel(new ResourceModel("register.email"));
		form.add(email);
		
		PasswordTextField password1 = new PasswordTextField("password1", new PropertyModel<String>(this, "password1"));
		password1.add(StringValidator.minimumLength(8));
		password1.setLabel(new ResourceModel("register.password1"));
		form.add(password1);
		
		PasswordTextField password2 = new PasswordTextField("password2", new PropertyModel<String>(this, "password2"));
		password2.setLabel(new ResourceModel("register.password2"));
		form.add(password2);
		
		form.add(new EqualPasswordInputValidator(password1, password2));
		AjaxSubmitLink link = new AjaxSubmitLink("link") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(RegisterPanel.this);
			}
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				user.setPasword(RegisterPanel.this.password1);
				User  temp = usersDao.loadById(user.getUserId());
				if(temp != null) {
					userId.error(getString("register.error.userAlreadyExist1"));
					return;
				}
				usersDao.save(user);
				try {
					MiniMap<String, String> variables = new MiniMap<String, String>(3);
					variables.put("name", user.getName());
					variables.put("id", user.getLogin());
					variables.put("password", user.getPasword());
					TextTemplate template = new PackageTextTemplate(RegisterPanel.class, "Register.template").interpolate(variables);
					MailUtil.sendEmail(
							user.getEmail(), 
							"Your registration!", 
							template.getString());
				} catch (Exception e) {
					//TODO: clean the crap here
					e.printStackTrace();
				}
				onRegisterSuccess(target, form);
			}
			
		};
		form.add(link);
		feedback = new FeedbackPanel("feedback", new ComponentFeedbackMessageFilter(form));
		feedback.setOutputMarkupId(true);
		add(feedback);
	}
	
	protected abstract void onRegisterSuccess(AjaxRequestTarget target, Form<?> form);
	
	private TextField<String> createTextField(String name, boolean required) {
		TextField<String> textField = new TextField<String>(name);
		textField.setRequired(required);
		return textField;
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public IUsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(IUsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}
}
